module.exports = (grunt) => {
    // 模板html文件需要的数据
    const data = {
        menus: [
            {
                name: "Home",
                icon: "aperture",
                link: "index.html",
            },
            {
                name: "Features",
                link: "features.html",
            },
            {
                name: "About",
                link: "about.html",
            },
            {
                name: "Contact",
                link: "#",
                children: [
                    {
                        name: "Twitter",
                        link: "https://twitter.com/w_zce",
                    },
                    {
                        name: "About",
                        link: "https://weibo.com/zceme",
                    },
                    {
                        name: "divider",
                    },
                    {
                        name: "About",
                        link: "https://github.com/zce",
                    },
                ],
            },
        ],
        pkg: require("./package.json"),
        date: new Date(),
    };
    const sass = require("node-sass");
    grunt.loadNpmTasks("grunt-contrib-clean");
    grunt.loadNpmTasks("grunt-contrib-copy");
    grunt.loadNpmTasks("grunt-babel");
    grunt.loadNpmTasks("grunt-sass");
    grunt.loadNpmTasks("grunt-swigtemplates");
    grunt.loadNpmTasks("grunt-contrib-imagemin");
    grunt.loadNpmTasks("grunt-usemin");
    grunt.loadNpmTasks("grunt-contrib-htmlmin");
    grunt.loadNpmTasks("grunt-contrib-cssmin");
    grunt.loadNpmTasks("grunt-contrib-uglify");
    grunt.loadNpmTasks("grunt-contrib-concat");
    grunt.loadNpmTasks("grunt-contrib-watch");
    grunt.loadNpmTasks("grunt-browser-sync");
    grunt.initConfig({
        // 清理dist目录
        clean: {
            main: {
                src: ["dist"],
            },
        },
        // 拷贝静态资源文件
        copy: {
            main: {
                expand: true,
                cwd: "public",
                src: "**",
                dest: "dist",
            },
        },
        // 编译js
        babel: {
            options: {
                sourceMap: false,
                presets: ["@babel/preset-env"],
            },
            main: {
                expand: true,
                cwd: "src/assets/scripts",
                src: "*.js",
                dest: "dist/assets/scripts",
            },
        },
        // 编译scss
        sass: {
            options: {
                implementation: sass,
                sourceMap: false,
                outputStyle: "expanded",
            },
            main: {
                expand: true,
                ext: ".css",
                cwd: "src/assets/styles",
                src: "*.scss",
                dest: "dist/assets/styles",
            },
        },
        // 编译html
        swigtemplates: {
            options: {
                defaultContext: data,
                templatesDir: "src",
            },
            main: {
                src: ["src/*.html"],
                dest: "dist",
            },
        },
        // 压缩图片和字体
        imagemin: {
            mainImages: {
                expand: true,
                cwd: "src/assets/images",
                src: "**",
                dest: "dist/assets/images",
            },
            mainFonts: {
                expand: true,
                cwd: "src/assets/fonts",
                src: "**",
                dest: "dist/assets/fonts",
            },
        },
        // 资源合并
        useminPrepare: {
            html: "dist/*.html",
            options: {
                dest: "dist",
                root: ["dist", "."],
            },
        },
        // 资源合并
        usemin: {
            html: "dist/*.html",
        },
        // 压缩html
        htmlmin: {
            options: {
                removeComments: true,
                collapseWhitespace: true,
                minifyCSS: true,
                minifyJS: true,
            },
            main: {
                expand: true,
                cwd: "dist",
                src: "*.html",
                dest: "dist",
            },
        },
        // 监听文件变化，对其进行编译处理
        watch: {
            mainScss: {
                files: "src/assets/styles/*.scss",
                tasks: ["sass"],
            },
            mainJs: {
                files: "src/assets/scripts/*.js",
                tasks: ["babel"],
            },
        },
        // 开启静态服务器
        browserSync: {
            main: {
                open: false,
                notify: false,
                bsFiles: {
                    src: ["dist", "src", "public"],
                },
                options: {
                    watchTask: true,
                    server: {
                        baseDir: ["dist", "src", "public"],
                        routes: {
                            "/node_modules": "node_modules",
                        },
                    },
                },
            },
        },
    });

    grunt.registerTask("compile", ["babel", "swigtemplates", "sass"]);
    // 开发任务
    grunt.registerTask("serve", ["clean", "compile", "browserSync", "watch"]);
    // 生产任务
    grunt.registerTask("build", [
        "clean",
        "copy",
        "compile",
        "imagemin",
        "useminPrepare",
        "concat",
        "cssmin",
        "uglify",
        "usemin",
        "htmlmin",
    ]);
};
